<?php echo $this->headLink()->appendStylesheet($this->Css('jtip.css'))?>
<?php echo $this->headScript()->appendFile($this->js('jquery/jquery.js'))
	->appendFile($this->js('jquery/jquery.template.js'))
	->appendFile($this->js('jquery/plugins/form/jquery.form.js'))
	->appendFile($this->js('jquery/plugins/jquery.blockUI.js'))
	->appendFile($this->js('jquery/plugins/jtip.js'))
	->captureStart();	
?>
var all_class = 1; // 各班設定
var class_data = null; // 班名
var subject_data = null; // 科目名
var yetset_subject = null;
$(document).ready(function(){
	
	initData();
	initSubjectData();		
	
	$("#enbtn").click(function(){
		if (all_class){			
			$(this).css('background-color','#fe8').val('啟用全年級設定');
			all_class = 0;
		}
		else {
			$(this).css('background-color','').val('啟用各班設定');
			all_class = 1;
		}
		initClass();
		initSubject();
	});
		
	$(".refresh").change(function(){
		initClass();
		initSubjectData();	
	});
	
	$("#sel_class").change(function(){
		initSubject();		
	});
	
	
});

function initData(){
	var year_semester = $("#year_semester").val();
	var url = '<?php echo $this->baseScript ?>/getyear/year_semester/'+year_semester;
	$.getJSON(url,function(data){
		class_data = data;
		initClass();
	});
}

function initSubjectData(){
	var year_semester = $("#year_semester").val();
	if ($("#sel_grade").val())
		var sel_grade = $("#sel_grade").val();
	else
		var sel_grade = 1;
	
	// 取得學科資料
	var url = '<?php echo $this->baseScript ?>/getsubject/year_semester/'+year_semester+'/grade/'+sel_grade;	
	$.getJSON(url,function(data){
		subject_data = data.data;
		yetset_subject = data.yetset;
		initSubject();
	});
	
}

// 初始化班級
function initClass() {
	$("#sel_class").empty();
	var year_semester = $("#year_semester").val();
	if ($("#sel_grade").val())
		var sel_grade = $("#sel_grade").val();
	else
		var sel_grade = 1;	
	if (all_class){  //全年級設定
		$("#sel_class").hide();		
	} else {
		$("#sel_class").show();		
		var data = class_data[sel_grade];		
		var t1 = $.template("<option value='${id}'>${val}</option>");
		$.each(data ,function(){
			$("#sel_class").append(t1,{
				id  : this.class_sn,	
				val : this.chinese_name+'班'
				});
			});		
	}			
}


// 初始化科目
function initSubject(){
	$("#settable  tbody").empty();	
	if ($("#sel_class").val())
		var title = $("#sel_grade :selected").text()+$("#sel_class :selected").text();
	else 
		var title = $("#sel_grade :selected").text()+'全年級';
			
	$("#subtitle").html(title+' 科目設定');
	
	var nor =  '<td >${name}</td><td>${scored}</td><td>${scored_mode}</td><td>${sections}</td><td>${rate}</td><td>${rank}</td><td sn="${sn}">${delbtn} </td>';
	var t1 = $.template('<tr id="${id}"><td rowspan="${i}" style="text-align:right">${scope}</td>'+nor+'</tr>${appendRow}');
	var t2 = $.template('<tr id="${id}">'+nor+'</tr>${appendRow}');	
	//var len = subject_data.length;
	
	var r =0;
	$.each(subject_data,function(s,v){				
		vlen = v.length;				
		
		for(i=0; i < v.length ;i++){					
			if (i == 0) var st = t1; else st = t2;
			
			var scope_rank = v[i].scope_rank;
			var rank = v[i].rank;
			
			if (vlen>1){
				var arank = scope_rank+'-'+rank;
			} else {
			 	var arank = scope_rank;
			}
			if (v[i].required==0)
				var delbtn = '<a href="#" class="delete">刪除</a>';
			if (v[i].scored == 'Y')
				var scored = "<img src='<?php echo $this->baseUrl ?>/html/images/ok.png' />"
			else
				scored = '';
			if (v[i].scored_mode == '1')
				var scored_mode = "<img src='<?php echo $this->baseUrl ?>/html/images/ok.png' />"
			else
				var scored_mode = '';
				
			$("#settable  tbody").append(st, {
				id   : 'sn-'+v[i].sn,
				i	 : vlen,
				sn   : v[i].sn,
				scope: s ,
				img2  : '<?php echo $this->baseUrl ?>/html/images/edit.png',
				scored : scored,
				scored_mode : scored_mode,				
				name : v[i].name,
				sections: v[i].sections,
				rate : v[i].rate,
				rank : arank,
				delbtn: delbtn
				});
		}
	});
	$("#settable  tr:even").css("background", "#fee");
	
	// 未設定科目
	if (yetset_subject){
		$("#addtable").show();
		$("#addtable ul").empty();
		var t2 = $.template('<li id="y-${id}">${name},<a href="#">加入</a></li>');
		$.each(yetset_subject,function(i,v){
			$("#addtable ul").append(t2,{
				id: i,
				name : v.scope+'-'+v.name
			});
		});
	}
	else
		$("#addtable").hide();
}

<?php echo $this->headScript()->captureEnd() ?>

<?php echo $this->headStyle()->captureStart() ?>
	#settable {float:left;width:80%;margin:5px}
	#settable table {width:100%}
	#settable td {padding:3px;text-align:center}	
	#addtable {float:right;width:18%}
	#addtable h1 {font-size:150%}
<?php echo $this->headStyle()->captureEnd() ?>

<div class="main_body">
<form action="<?php echo $this->baseScript?>/edit" method="post" id="myform">
<div id="settable">
<table width="85%" border="1" >
<thead>
<tr>
<th colspan="8" style="text-align:center" id="sethead">
<div style="float:left;text-align:center;width:70%">
<select name="year_semester" class="refresh" id="year_semester">
<?php foreach($this->YearSemester() as $val): ?>
	<?$id = $val['year'].'-'.$val['semester'] ?>		
	<option value="<?php echo $id?>" <?php if ($id == $this->year_semester): ?>selected<?php endif ?> ><?php echo $val['year'] ?>學年第<?php echo $val['semester'] ?>學期</option>
<?php endforeach; ?>		
</select> 
<select name='sel_grade' id="sel_grade" class="refresh" >	
<?php foreach ($this->ClassGrade() as $id=>$val): ?>
	<option value="<?php echo  $id ?>"><?php echo $val ?>級</option>
<?php endforeach; ?>
</select>
<select id="sel_class"  style="display:none">
</select>
 科目設定表
</div>
<div style="float:right"><input id="enbtn" type="button"  value="啟用各班設定" />
<span class="formInfo"><a href="<?php echo $this->baseScript?>/info/id/1?width=250" class="jTip" id="one" name="啟用各班設定說明">?</a></span>
</div>
</th>
</tr>
<tr>
<th colspan="8">
<span id="subtitle" style="width:50%"></span> <input id="editbtn" type="button" value="修改" style="width:30%" />
</th>
</tr>
<tr>
<th>領域</th><th>科目名稱</th><th>計分</th><th>完整</th><th>節數</th><th>加權</th><th>排序</th><th>編修</th></tr>
</thead>
<tbody>

</tbody>
</table>
</div>
</form>
<div id="addtable">
<h1>未加入科目</h1>
<ul>
</ul>
</div>
</div>
